package pers.fulsun.demo.springcloud.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.fulsun.demo.springcloud.entity.User;
import pers.fulsun.demo.springcloud.mapper.UserMapper;
import pers.fulsun.demo.springcloud.service.UserService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fulsun
 * @since 2021-08-18
 */
@RestController
@RequestMapping("/user")
@Api(value = "UserController", description = "用户端模块前端控制器", tags = {"client"})
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增user
     *
     * @param user user对象
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加用户", notes = "添加用户", response = Boolean.class, produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User", paramType = "body"),
    })
    public boolean adduser(@RequestBody User user) {
        return this.userService.save(user);
    }

    /**
     * 根据id获取user
     *
     * @param id user的id
     * @return
     */
    @GetMapping("/{id}")
    public User getuserById(@PathVariable Integer id) {
        return this.userService.getById(id);
    }

    /**
     * 根据id删除user
     *
     * @param id user的id
     * @return
     */
    @DeleteMapping("/{id}")
    public boolean removeuser(@PathVariable Integer id) {
        return this.userService.removeById(id);
    }

    @GetMapping("/page")
    public IPage getClientPage(Page page, User user) {
        return this.userService.page(page, new QueryWrapper<>(user));
    }

    @GetMapping("/page2")
    public IPage<User> getClientPage2(Page page, User user) {
        return this.userMapper.selectUserPage(page, user);
    }
}

